package com.jk.hearthstone.core;

import com.jk.hearthstone.card.BaseCard;
import com.jk.hearthstone.card.biology.BaseBiology;
import com.jk.hearthstone.card.biology.entourage.BaseEntourage;
import com.jk.hearthstone.card.biology.hero.BaseHero;
import com.jk.hearthstone.data.Desktop;
import com.jk.hearthstone.data.History;

import java.util.LinkedList;

/**
 * 暴露在外面的Controller,控制整个Controller链
 * @author Administrator
 */
public class Controller implements Cloneable{
    private Desktop desktop;
    private History history;
    private LinkedList<BaseController> controllerChain;

    public Controller(Desktop desktop, History history){
        this.desktop = desktop;
        this.history = history;
        controllerChain = new LinkedList<>();
        controllerChain.addFirst(new DefaultController());
    }

    /**
     * 向控制链里面添加新的控制器
     * @param controller
     */
    public void appendController(BaseController controller){
        controllerChain.addFirst(controller);
    }

    /**
     * 移除控制链里面的指定控制器
     * @param controller
     */
    public void removeController(BaseController controller){
        controllerChain.remove(controller);
    }

    public boolean play(BaseCard card) {
        for (BaseController baseController : controllerChain) {
            if(!baseController.play(desktop,history,card)){
                return false;
            }
        }
        return true;
    }

    public boolean play(BaseCard card, BaseBiology target) {
        for (BaseController baseController : controllerChain) {
            if(!baseController.play(desktop,history,card,target)){
                return false;
            }
        }
        return true;
    }

    public void skill(BaseHero hero) {
        for (BaseController baseController : controllerChain) {
            baseController.skill(desktop,history,hero);
        }
    }

    public void skill(BaseHero hero, BaseBiology target) {
        for (BaseController baseController : controllerChain) {
            baseController.skill(desktop,history,hero,target);
        }
    }

    public void attack(BaseBiology attacker, BaseBiology target) {
        for (BaseController baseController : controllerChain) {
            baseController.attack(desktop,history,attacker,target);
        }
    }

    public void cure(BaseBiology target, Integer num) {
        for (BaseController baseController : controllerChain) {
            baseController.cure(desktop,history,target,num);
        }
    }

    public void hurt(BaseBiology target, Integer num) {
        for (BaseController baseController : controllerChain) {
            baseController.hurt(desktop,history,target,num);
        }
    }

    public void nextTurn() {
    }

    public Desktop getDesktop() {
        return desktop;
    }

    public void setDesktop(Desktop desktop) {
        this.desktop = desktop;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Object result = super.clone();
        for (BaseEntourage baseEntourage : this.desktop.myEntourage) {
            //先删除旧链上，场怪的控制器
            removeController(baseEntourage.getMyController());
            //初始化场怪的myController
            baseEntourage.initMyController();
        }
        //桌面深度克隆
        ((Controller)result).desktop = (Desktop) this.desktop.clone();
        //控制链浅度克隆
        LinkedList<BaseController> controllerChain = new LinkedList<>();
        for (BaseController baseController : this.controllerChain) {
            controllerChain.addLast(baseController);
        }
        //在新链上append场怪
        for (BaseEntourage baseEntourage : ((Controller) result).desktop.myEntourage) {
            controllerChain.addLast(baseEntourage.getMyController());
        }
        ((Controller)result).controllerChain = controllerChain;
        return result;
    }
}
